package com.inspectortime.webapp.inspector.scheduleRule;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.inspectortime.domain.ScheduleRule;
import com.mkode.util.java.lang.ClassUtils;

public class ScheduleRuleValidator implements Validator {

	
	public boolean supports(Class clazz) {
		return ClassUtils.isInstanceOf(clazz, ScheduleRule.class);
	}

	public void validate(Object obj, Errors errors) {
		ScheduleRule scheduleRule = (ScheduleRule) obj;
		
		if (scheduleRule.getStartHour() > scheduleRule.getEndHour()) {
			errors.rejectValue("startMinute", "addEditScheduleRule.startEndTime.invalid");
		} else if (scheduleRule.getStartHour() == scheduleRule.getEndHour() &&
					scheduleRule.getStartMinute() >= scheduleRule.getEndMinute()) {
			errors.rejectValue("startMinute", "addEditScheduleRule.startEndTime.invalid");
		}
		
		
		if (scheduleRule.getStartDate() != null && scheduleRule.getEndDate() != null) {
			if (scheduleRule.getStartDate().after(scheduleRule.getEndDate())) {
				errors.rejectValue("endDate", "addEditScheduleRule.startEndDate.invalid");
			}
		}
		
	}
}
